//
//  PKCommunityMainViewController.m
//  PianKe
//
//  Created by lanou on 15/12/21.
//  Copyright © 2015年 KFC. All rights reserved.

#import "PKCommunityMainViewController.h"
#import "PKCommunityTableViewController.h"
#import "PKCommunityWebViewController.h"

@interface PKCommunityMainViewController ()


@property (strong, nonatomic) PKCommunityTableViewController *PostHot;

// 话题下面上的segment
@property (strong, nonatomic) UISegmentedControl *Postsegment;


@end

@implementation PKCommunityMainViewController

#pragma mark ---- 注销通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"pushWebView" object:nil];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    // 分割控件1
    NSArray *segmentArray = @[@"热门",@"最新"];
    _Postsegment = [[UISegmentedControl alloc] initWithItems:segmentArray];
    _Postsegment.frame = CGRectMake(kScreenWidth / 4 , 30*kHeightBase, kScreenWidth / 2, 30*kHeightBase);
    _Postsegment.layer.cornerRadius = 10;
    _Postsegment.backgroundColor = [UIColor whiteColor];
    _Postsegment.tintColor = [UIColor tanguolu];
    [_Postsegment addTarget:self action:@selector(changeNewAndHot:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_Postsegment];
    
    
    #pragma mark  ------ 展示第一页数据的listTableView
    _PostHot = [[PKCommunityTableViewController alloc] init];
    _PostHot.urlString = @"http://api2.pianke.me/group/posts_hotlist";
    _PostHot.sortType = @"hot";
    _PostHot.tableView.frame = CGRectMake(0, 65, kScreenWidth, kScreenHeight - 64);
    [self.view addSubview:_PostHot.tableView];

    
    _Postsegment.selectedSegmentIndex = 0;
    
#pragma mark ---- 注册通知,接收来自PKCommunityTableViewController的通知,去跳转
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"pushWebView" object:nil];
  
}

- (void) notificationAction:(NSNotification *)notification
{
    NSString *contentid = notification.userInfo[@"contentid"];
    // 跳转web
    PKCommunityWebViewController *webView = [[PKCommunityWebViewController alloc] init];
    webView.contentid = contentid;
    [self.navigationController pushViewController:webView animated:YES];
    
}




- (void)changeNewAndHot:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 1) {
        
        // 点击了new
        _PostHot.sortType = @"addtime";
        [_PostHot reloadTableView];
        

        
    } else
    {
        _PostHot.sortType = @"hot";
        [_PostHot reloadTableView];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
